package com.ddxz.hospital.controller;

import com.ddxz.hospital.dto.*;
import com.ddxz.hospital.service.InputService;
import com.ddxz.tool.web.dto.DdxzResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/9/23
 */

@RestController
@Api("数据输入")
public class InputController {
    @ApiOperation(value = "保存病人信息", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/patient/save", method = RequestMethod.POST)
    public DdxzResponse patientSave(@RequestBody PatientVO patient,
                                    @RequestHeader(name="Authorization") String token) {
        return service.patientSave(token, patient);
    }

    @ApiOperation(value = "保存住院信息", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/consult/{patientId}/save", method = RequestMethod.POST)
    public DdxzResponse consultSave(@RequestBody ConsultVO consult,
                                    @PathVariable("patientId") String patientId,
                                    @RequestHeader(name="Authorization") String token) {
        return service.consultSave(token, patientId, consult);
    }

    @ApiOperation(value = "保存随访信息", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/course/{patientId}/save", method = RequestMethod.POST)
    public DdxzResponse courseSave(@RequestBody CourseVO course,
                                   @PathVariable("patientId") String patientId,
                                   @RequestHeader(name="Authorization") String token) {
        return service.courseSave(token, patientId, course);
    }

    @ApiOperation(value = "查询病人信息", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/patient/{patientId}/detail", method = RequestMethod.GET)
    public DdxzResponse patientDetail(@PathVariable("patientId") String patientId,
                                      @RequestHeader(name="Authorization") String token) {
        return service.patientDetail(token, patientId);
    }

    @ApiOperation(value = "查询住院信息", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/consult/{consultId}/detail", method = RequestMethod.GET)
    public DdxzResponse consultDetail(@PathVariable("consultId") String consultId,
                                      @RequestHeader(name="Authorization") String token) {
        return service.consultDetail(token, consultId);
    }

    @ApiOperation(value = "查询随访信息", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/course/{courseId}/detail", method = RequestMethod.GET)
    public DdxzResponse courseDetail(@PathVariable("courseId") String courseId,
                                     @RequestHeader(name="Authorization") String token) {
        return service.courseDetail(token, courseId);
    }

    @ApiOperation(value = "检索病人清单", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/patient/list", method = RequestMethod.GET)
    public DdxzResponse patientSearch(@RequestHeader(name = "Authorization") String token,
                                      @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                                      @RequestParam(name = "size",required = false,defaultValue = "10") Integer size,
                                      @RequestParam(name = "name",required = false,defaultValue = "") String name,
                                      @RequestParam(name = "phone",required = false,defaultValue = "") String phone) {
        return service.patientList(token, page, size, name, phone);
    }

    @ApiOperation(value = "检索治疗清单", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/cure/{patientId}/list", method = RequestMethod.GET)
    public DdxzResponse cureSearch(@RequestHeader(name = "Authorization") String token,
                                   @PathVariable("patientId") String patientId,
                                   @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                                   @RequestParam(name = "size",required = false,defaultValue = "10") Integer size,
                                   @RequestParam(name = "type",required = false,defaultValue = "0") Integer type,
                                   @RequestParam(name = "beginDate",required = false,defaultValue = "") String beginDate,
                                   @RequestParam(name = "endDate",required = false,defaultValue = "") String endDate) {
        return service.cureList(token, patientId, beginDate, endDate, type, page, size);
    }

    @ApiOperation(value = "登陆系统", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public DdxzResponse login(@RequestBody LoginInfo loginInfo) {
        return service.login(loginInfo);
    }

    @ApiOperation(value = "更新令牌", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/pong", method = RequestMethod.PUT)
    public DdxzResponse pong(@RequestHeader(name = "Authorization") String token) {
        return service.pong(token);
    }

    @ApiOperation(value = "移除病人信息", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/patient/{patientId}/remove", method = RequestMethod.PUT)
    public DdxzResponse removePatient(@PathVariable("patientId") String patientId,
                                      @RequestHeader("Authorization") String token) {
        return service.removePatient(token, patientId);
    }

    @ApiOperation(value = "移除治疗信息", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/cure/{cureId}/remove", method = RequestMethod.PUT)
    public DdxzResponse removeCure(@PathVariable("cureId") String curId,
                                   @RequestHeader("Authorization") String token) {
        return service.removeCure(token, curId);
    }

    @ApiOperation(value = "导入病人信息", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/file/import", method = RequestMethod.POST)
    public DdxzResponse importPatient(MultipartFile file,
                                      @RequestHeader("Authorization") String token) {
        return service.importPatient(token, file);
    }

    @ApiOperation(value = "导入住院信息", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/file/consult", method = RequestMethod.POST)
    public DdxzResponse importConsult(@RequestBody ConsultFile file/*MultipartFile file,
                                      @RequestHeader("Authorization") String token*/) {
//        request.getHeader("file");
//        try(InputStream is = request.getInputStream();
//            FileOutputStream fos = new FileOutputStream("aaa.bbb")) {
//            byte[] content = new byte[4096];
//            int len;
//            while ((len = is.read(content)) > 0) {
//                fos.write(content, 0, len);
//            }
//            fos.flush();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return service.importConsult(null, file);
        return service.importConsult(file);
    }

    @ApiOperation(value = "导入随访信息", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/file/course", method = RequestMethod.POST)
    public DdxzResponse importCourse(MultipartFile file,
                                      @RequestHeader("Authorization") String token) {
        return service.importCourse(token, file);
    }

    private final InputService service;

    public InputController(InputService service) {
        this.service = service;
    }
}
